package com.example.LibrarySystem.interceptor;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.CorsRegistry;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

/**
 * 注册自定义拦截器
 */

@Configuration
public class Interceptor implements WebMvcConfigurer {

    /**
     * 拦截器 在 依赖注入之前 所以 bean 先注册到IOC容器
     */
    @Bean
    public TokenInterceptor getTokenInterceptor() {
        return new TokenInterceptor();
    }

    @Bean
    public RoleInterceptor getRoleInterceptor() {
        return new RoleInterceptor();
    }

    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        // 添加 token拦截器
        registry.addInterceptor(getTokenInterceptor())
                .addPathPatterns("/**") // 拦截所有请求路径
                .excludePathPatterns("/user/login", "/user/register","/files/**","/files/shows/**"); // 放行 登录 注册 文件接口

        // 添加角色拦截器
        registry.addInterceptor(getRoleInterceptor())
                .addPathPatterns("/**")
                .excludePathPatterns("/user/login", "/user/register","/files/**","/files/shows/**");
    }
}
